<template>
     <div style='width:100%;text-align:center;overflow:hidden;' ref="kmDiv">
        <div  id='CameraCtl' style="width:800px; height: 500px;margin-left:auto;margin-right:auto;position:relative">  </div>
       <div style='width:100%;text-align:left;padding-left:20px;display:flex;flex-direction: row;' class='bottoms'>
            <div style="line-height:60px">摄像头:</div>
            <el-select v-model="CamID" placeholder="请选择" style="margin-top:10px">
                <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
            </el-select>

            <div style="line-height:60px">去底色:</div>
            <el-select v-model="isds" placeholder="请选择" style="margin-top:10px">
                <el-option
                   
                    key="N"
                    label="否"
                    value="N">
                </el-option>
                <el-option
                   
                    key="Y"
                    label="是"
                    value="Y">
                </el-option>
            </el-select>
            <div style="line-height:60px">去黑边:</div>
            <el-select v-model="ishb" placeholder="请选择" style="margin-top:10px">
                <el-option
                   
                    key="N"
                    label="否"
                    value="N">
                </el-option>
                <el-option
                   
                    key="Y"
                    label="是"
                    value="Y">
                </el-option>
            </el-select>
           <el-button type="primary" @click="scan" v-if="names.length<=1" style='margin-left:100px;margin-top:10px;'>上传</el-button>
            <template v-else>
                <el-button type="primary" style='margin-top:5px' class="uploads" @click="changeName(item)"  v-for="(item,index) in names" :key="index">{{item}}</el-button>
            </template>
           <el-button type="primary" @click="closes()" style="margin-top:10px;">关闭</el-button>
       </div>
   </div>
   
</template>
<script>
export default {
    name:"high-shotkm",
    props:["show","appid","showType","params"],
    data(){
        return{
            socket:null,
            heartTimerId:null,
            isSocketConnect:false,
            intervalId:null,
            isOpenMainCamera:false,
            MainCanvas:null,
            MainContext:null,
            MainCamCutMode:0,//手动裁边
            pMainShowStartX:0,
            pMainShowStartY:0,
            pALastX:0,
            pALastY:0,
            pACurrentX:0,
            pACurrentY:0,
            name:"",
            names:[],
            img:"",
            CamID:"",
            options:[],
            isMouseDown:false,
            isds:"N",
            ishb:"N"
        }
    },
    created(){
        try{
            if(this.params&&this.params.names){
                this.names=this.params.names.split(",");
            }
            if(this.names.length==1){
                this.name=this.names[0]
            }
            if(this.names.length==0){
                this.name=this.params.name;
            }
        }catch(e){}
    },
    mounted(){
        var obj = document.getElementById("CameraCtl");
        console.log(obj,"obj===============================")
        this.Cam_ControlInit(obj,0, 0, 600, 400);
    },
    watch:{
        isds(val){
            if(val=="Y"){
                this.Cam_SetDeleteBgColor(1)
            }else{
                this.Cam_SetDeleteBgColor(0)
            }
        },
        ishb(val){
            if(val=="Y"){
                this.SetDeleteBlackEdge(1)
            }else{
                this.SetDeleteBlackEdge(0)
            }
        }
    },
    methods:{
        uploads(){
          let name=this.params.name;
          this.$post(this.appid,"/api/file/fileUploadBase64",{data:this.img.replace("data:image/jpeg;base64,",""),model:this.params.model,type:this.params.type,id:this.params.id,token:this.params.token,name:this.name}).then(data=>{
               console.log("uploadImg==========================");
               console.log(data);
               this.$emit("uploadImg",JSON.stringify(data))
           });
       },
        changeName(item){
            this.name=item;
            this.scan();
        },
        scan(){
            if (this.MainCamCutMode == 2) {
                var rectx, recty, rectw, recth;
                if (this.pALastX > this.pACurrentX)
                    rectx = this.pACurrentX;
                else
                    rectx = this.pALastX;
                if (this.pALastY > this.pACurrentY)
                    recty = this.pACurrentY;
                else
                    recty = this.pALastY;
                rectw = Math.abs(this.pACurrentX - this.pALastX);
                recth = Math.abs(this.pACurrentY - this.pALastY);

                this.SetCutRect(rectx, recty, rectw, recth);  //手动裁剪区域
            }    
            this.CaptureImage("");
        },
        heartCheck() {
            let that=this;
            clearInterval(this.heartTimerId);
            this.heartTimerId = setInterval(function () {
                if (that.isSocketConnect) {
                    var aDataArray = new Uint8Array(3);
                    aDataArray[0] = 0x11;
                    aDataArray[1] = 0x11;
                    aDataArray[2] = 0x11;
                    that.socket.send(aDataArray.buffer);
                   // console.log("heartCheck...........");
                }
            }, 6000);
        },
        Cam_ControlInit(documentObj, mX, mY, mwidth, mheight){
            this.WebSocketConnect();
            this.InitCanvas(documentObj, mX, mY, mwidth, mheight);
        },
        Cam_GetDevCount() {
            this.GetDevCount();
        },
        GetDevCount() {
            var aDataArray = new Uint8Array(3);
            aDataArray[0] = 0x77;
            aDataArray[1] = 0x88;
            aDataArray[2] = 0x50;  
            this.socket.send(aDataArray.buffer);
        },
        reconnect() {

            clearInterval(this.intervalId);
            this.intervalId = setInterval(function () {
                if (this.isSocketConnect == false) {
                    this.WebSocketConnect();
                    console.log("reconnect...")
                }
                console.log("reconnectTimer.........!")
            }, 3000);
        },
        GetDevCountAndNameResultCB(devCount, devNameArr){//返回获取的设备数目及设备名称
            if (devCount > 0) {

                this.options=[];
                for (var i = 0; i < devCount; i++) {
                    let option={
                        label:devNameArr[i],
                        value:i
                    }
                    this.options.push(option)
                }
                this.CamID=0;
               

                //获取分辨率
                this.GetResolution(this.CamID);  

            }
            else {
                alert("没有发现合适的设备！");
            }
        },
        GetResolution(iCamNo) {
            var aDataArray = new Uint8Array(4);
            aDataArray[0] = 0x77;
            aDataArray[1] = 0x88;
            aDataArray[2] = 0x51;
            aDataArray[3] = iCamNo;
            this.socket.send(aDataArray.buffer);
        },
        GetResolutionResultCB(resCount, resArr){//返回获取的设备分辨率信息
            if (resCount > 0) {
                let restrText="";
                let restrText2="";
                for (var i = 0; i < resCount; i++) {
                     if(i==0){
                        restrText2=resArr[i] 
                     }
                    //默认500万分辨率打开
                    if (resArr[i] == "2592*1944") {
                        restrText="2592*1944";
                    }
                }
                restrText=restrText=="" ? restrText2:restrText;
                //打开摄像头
                this.Cam_Close();
                this.sleep(100);
                var restr = restrText;
                var pos = restr.lastIndexOf("*");
                var width = parseInt(restr.substring(0, pos));
                var height = parseInt(restr.substring(pos + 1, restr.length));
                
                this.OpenCamera(this.CamID, width, height);
            }
            else {
                alert("获取分辨率信息失败！");
            }
        },
        sleep(milliSeconds) {
            var startTime = new Date().getTime();
            while (new Date().getTime() < startTime + milliSeconds);
        },
        Cam_Close() {

            this.CloseCamera();  
            this.isOpenMainCamera = false;
        },
        CloseCamera() {
            if (this.isSocketConnect) {
                var aDataArray = new Uint8Array(4);
                aDataArray[0] = 0x77;
                aDataArray[1] = 0x88;
                aDataArray[2] = 0x02;  //关闭摄像头
                aDataArray[3] = 0x00;
                this.socket.send(aDataArray.buffer);
            }
        },
        OpenCamera(iCamNo, width, height) {

            this.pALastX = 0;
            this.pALastY = 0;
            this.pACurrentX = 0;
            this.pACurrentY = 0;

            if (this.isSocketConnect) {
                var aDataArray = new Uint8Array(12);
                aDataArray[0] = 0x77;
                aDataArray[1] = 0x88;
                aDataArray[2] = 0x01;   //打开摄像头
                aDataArray[3] = iCamNo;
                aDataArray[4] = this.MainCanvas.width >> 8 & 0xff;
                aDataArray[5] = this.MainCanvas.width & 0xff;
                aDataArray[6] = this.MainCanvas.height >> 8 & 0xff;
                aDataArray[7] = this.MainCanvas.height & 0xff;
                aDataArray[8] = width >> 8 & 0xff;
                aDataArray[9] = width & 0xff;
                aDataArray[10] = height >> 8 & 0xff;
                aDataArray[11] = height & 0xff;

                this.socket.send(aDataArray.buffer);

                this.sleep(500);
                this.MainCamCutMode = 2; 
                var aDataArray2 = new Uint8Array(5);
                aDataArray2[0] = 0x77;
                aDataArray2[1] = 0x88;
                aDataArray2[2] = 0x11;  //设置裁剪模式
                aDataArray2[3] = 0x00;
                aDataArray2[4] = 2;
                this.socket.send(aDataArray2.buffer);     
            }
        },
        GetCameraOnOffStatus(status) {
            if (status == 0) {
                //ShowInfo("设备开启成功");
                //Cam_Rotate(1);   // 0-> 0度, 1->90度, 2->180度, 3->270度
            }
            else {
               // ShowInfo("设备开启失败!");
            }

        },
        GetCaptrueImgResultCB( flag, path , base64Str) {
            if (flag == 0) {
                //console.log(base64Str)
                this.img=base64Str
                if (path == "") {
                   // ShowInfo("拍照成功");
                   this.uploads();
                }
                else {
                   // ShowInfo("拍照成功，图片保存位置：" + path);
                }
            }
            else {
               // ShowInfo("拍照失败!");
            }

        },
        GetIdCardInfoResultCB(flag, Name, Sex, Nation, Born, Address, CardNum, IssuedAt, EffectedDate, CardImgPath, CardImgBase64) {
            
            if (flag == 0) {
              
                //ShowInfo("读卡成功2");
            }
            else {
                //ShowInfo("读卡失败!");
            }

        },
        GetIdCardInfoAndImgResultCB(flag, Name, Sex, Nation, Born, Address, CardNum, IssuedAt, EffectedDate, CardImgPath, CardHeadImgBase64, CardImgBase64) {
        
            if (flag == 0) {
                
            }
            else {
            }

        },
        HttpResultCB(flag, ResultStr) {
            if (flag == 0) {
                //alert("上传成功 :" + ResultStr);
            }
            else {
               // alert("上传失败！");
            }
        },
        QrBarCodeRecogResultCB(flag,codeStr) {//条码二维码识别返回结果
            if (flag == 0){
               // ShowInfo("条码/二维码 识别结果:" + codeStr);
            }else{

            }
               // ShowInfo("未识别到内容!");
        },
        AddImgFileToPDFResultCB(flag, base64Str)
        {
            if (flag == 0) {
               // ShowInfo("添加合并的PDF文件成功");
                var obj = document.getElementById("CameraPhoto");
                obj.src = "data:;base64," + base64Str;
            }
            else {
                //ShowInfo("添加合并的PDF文件失败!");
            }
        },
        InitCanvas(DivMainBox, mX, mY, mwidth, mheight) {

            //var DivMainBox = $("CameraCtl");

            if (mwidth != 0 && mheight != 0) {
                this.MainCanvas = document.createElement("canvas");
                this.MainCanvas.style.border = "solid 1px #A0A0A0";
                this.MainCanvas.id = "MainCamCanvas";
                this.MainCanvas.width = mwidth;
                this.MainCanvas.height = mheight;
                this.MainContext = this.MainCanvas.getContext("2d");
                DivMainBox.appendChild(this.MainCanvas);      //添加画布
                this.MainCanvas.onmousedown = this.MainCanvasMouseDown;
                this.MainCanvas.onmousemove = this.MainCanvasMouseMove;
                this.MainCanvas.onmouseup = this.MainCanvasMouseUp;
                this.MainCanvas.onmouseout = this.MainCanvasMouseOut;
                this.addEvent(this.MainCanvas, 'mousewheel', this.onMouseWheel);
                this.addEvent(this.MainCanvas, 'DOMMouseScroll', this.onMouseWheel);

            }

        },
        onMouseWheel(ev) { /*当鼠标滚轮事件发生时，执行一些操作*/
            var ev = ev || window.event;
            var down = true;
           
            down = ev.wheelDelta ? ev.wheelDelta < 0 : ev.detail > 0;
            if (down) {
                this.ZoomOut();
                //per += 0.05;
                //console.log("onMouseWheel down");
            } else {
                this.ZoomIn();
                //per -= 0.05;
                //console.log("onMouseWheel up");
            }
            //    if (ev.preventDefault) { /*FF 和 Chrome*/
            //        ev.preventDefault(); // 阻止默认事件
            //    }
            return false;
        },
        MainCanvasMouseUp(e) {
            this.isMouseDown = false;
        },
        MainCanvasMouseOut(e) {
            this.isMouseDown = false;
        },
        MainCanvasMouseDown(e) {
            console.log("MainCanvasMouseDown======================"+this.MainCamCutMode)
            this.isMouseDown = true;

            let width=document.body.clientWidth;
            let width2=(width-width*0.1)*0.8-800;
            console.log(e.pageX+"="+width2+"="+this.MainCanvas.offsetLeft)
            this.pALastX = e.pageX - this.MainCanvas.offsetLeft-width2;
            this.pALastY = e.pageY - this.MainCanvas.offsetTop-this.$refs.kmDiv.offsetTop-this.$refs.kmDiv.parentNode.parentNode.parentNode.offsetTop-this.$refs.kmDiv.parentNode.parentNode.offsetTop;
            if (this.MainCamCutMode == 2) {
                this.pACurrentX = this.pALastX;
                this.pACurrentY = this.pALastY;
            }
        },
        MainCanvasMouseMove(e) {
            let width=document.body.clientWidth;
            let width2=(width-width*0.1)*0.8-800;
            if (this.isMouseDown) {

                if (this.MainCamCutMode == 2) {

                    this.pACurrentX = e.pageX - this.MainCanvas.offsetLeft-width2;
                    this.pACurrentY = e.pageY - this.MainCanvas.offsetTop-this.$refs.kmDiv.offsetTop-this.$refs.kmDiv.parentNode.parentNode.parentNode.offsetTop-this.$refs.kmDiv.parentNode.parentNode.offsetTop;
         

                    //this.pACurrentX = e.pageX - this.MainCanvas.offsetLeft;
                   // this.pACurrentY = e.pageY - this.MainCanvas.offsetTop;

                    //console.log("MainCanvas:" + MainCanvas.offsetLeft + "," + MainCanvas.offsetTop);

                    var rectx,recty,rectw,recth ;
                    if (this.pALastX > this.pACurrentX)
                        rectx = this.pACurrentX;
                    else
                        rectx = this.pALastX;
                    if (this.pALastY > this.pACurrentY)
                        recty = this.pACurrentY;
                    else
                        recty = this.pALastY;
                    rectw = Math.abs(this.pACurrentX - this.pALastX);
                    recth = Math.abs(this.pACurrentY - this.pALastY);

                    this.SetCutRect(rectx, recty, rectw, recth);  //手动裁剪区域
                    //console.log(rectx + "," + recty + "," + rectw + "," + recth);
                }
                else {
                    this.pACurrentX = e.pageX - this.MainCanvas.offsetLeft-width2;
                    this.pACurrentY = e.pageY - this.MainCanvas.offsetTop-this.$refs.kmDiv.offsetTop-this.$refs.kmDiv.parentNode.parentNode.parentNode.offsetTop-this.$refs.kmDiv.parentNode.parentNode.offsetTop;
         
                    //this.pACurrentX = e.pageX - this.MainCanvas.offsetLeft;
                    //this.pACurrentY = e.pageY - this.MainCanvas.offsetTop;
                    var dx = pACurrentX - this.pALastX;
                    var dy = pACurrentY - this.pALastY;
                    var xdir = 0;
                    var ydir = 0;
                    if (dx < 0)
                        xdir = 0;
                    else
                        xdir = 1;
                    if (dy < 0)
                        ydir = 0;
                    else
                        ydir = 1;
                        this.pALastX = this.pACurrentX;
                        this.pALastY = this.pACurrentY;
                        this.MoveOffsetXY(Math.abs(dx), Math.abs(dy), xdir, ydir);            
                }      
            }
        },
        MoveOffsetXY(px,py,xdir,ydir) {
            if (this.isSocketConnect) {
                var aDataArray = new Uint8Array(10);
                aDataArray[0] = 0x77;
                aDataArray[1] = 0x88;
                aDataArray[2] = 0x09;  //移动
                aDataArray[3] = 0x00;
                aDataArray[4] = px >> 8 & 0xff;
                aDataArray[5] = px & 0xff;
                aDataArray[6] = py >> 8 & 0xff;
                aDataArray[7] = py & 0xff;
                aDataArray[8] = xdir ;
                aDataArray[9] = ydir ;
                this.socket.send(aDataArray.buffer);
            }
        },
        ZoomIn() {
            if (this.isSocketConnect) {
                var aDataArray = new Uint8Array(4);
                aDataArray[0] = 0x77;
                aDataArray[1] = 0x88;
                aDataArray[2] = 0x03;  //放大
                aDataArray[3] = 0x00;
                this.socket.send(aDataArray.buffer);
            }
        },
        ZoomOut() {
            if (this.isSocketConnect) {
                var aDataArray = new Uint8Array(4);
                aDataArray[0] = 0x77;
                aDataArray[1] = 0x88;
                aDataArray[2] = 0x04;  //缩小
                aDataArray[3] = 0x00;
                this.socket.send(aDataArray.buffer);
            }
        },
        addEvent(obj, xEvent, fn) {
            if (obj.attachEvent) {
                obj.attachEvent('on' + xEvent, fn);
            } else {
                obj.addEventListener(xEvent, fn, false);
            }
        },
        SetDeleteBlackEdge(flag) {
            if (this.isSocketConnect) {
                var aDataArray = new Uint8Array(4);
                aDataArray[0] = 0x77;
                aDataArray[1] = 0x88;
                aDataArray[2] = 0xA1;
                aDataArray[3] = flag;
                this.socket.send(aDataArray.buffer);
            }
        },
        Cam_SetDeleteBgColor(flag) {
            if (this.isSocketConnect) {
                var aDataArray = new Uint8Array(4);
                aDataArray[0] = 0x77;
                aDataArray[1] = 0x88;
                aDataArray[2] = 0xA2;
                aDataArray[3] = flag;
                this.socket.send(aDataArray.buffer);
            }
        },
        WebSocketConnect() {
            this.socket = new WebSocket("ws://127.0.0.1:22225");
            this.socket.binaryType = "arraybuffer";
            try {
                let that=this;
                this.socket.onopen = function (event) {

                    //heartCheck.reset().start(); 
                    that.heartCheck();
                    that.isSocketConnect = true;
                    clearInterval(that.intervalId);
                    //if (isOpenMainCamera == false)         
                    that.Cam_GetDevCount();
                    console.log("socket.onopen");

                   

                };

                this.socket.onclose = function (event) {

                    console.log("socket.onclose");
                    that.isSocketConnect = false;
                    that.reconnect();                           
                    //$("TextInfo").value = "websocket connect close!"
                };

                this.socket.onerror = function (event) {
                    that.isSocketConnect = false;
                    that.reconnect();                        
                    //$("TextInfo").value = "websocket connect error!"
                };


                this.socket.onmessage = function (event) {

                    //heartCheck.reset().start();      
                   
                    var rDataArr = new Uint8Array(event.data);
                    
                    if (rDataArr.length > 0) {


                        if (rDataArr[0] == 0x11 && rDataArr[1] == 0x11 && rDataArr[2] == 0x11) {
                            //console.log("heart socket!!!!");
                        }

                        if (rDataArr[0] == 0x55 && rDataArr[1] == 0x66) {
                            //摄像头数目返回
                            if (rDataArr[2] == 0x50) {
                                console.log("摄像头数目返回=============")
                                var devCount = rDataArr[3];
                                var devNameBufLen = rDataArr.length - 4;
                                var devNameData = new Uint8Array(devNameBufLen);
                                for (var i = 0; i < devNameBufLen; i++) {
                                    devNameData[i] = rDataArr[4 + i];
                                }
                                //var AllCamName = Uint8ArrayToString(devNameData);
                                var str = that.byteToString(devNameData);
                                var AllCamName = decodeURIComponent(str);
                                var camName = new Array();
                                camName = AllCamName.split('|');
                                that.GetDevCountAndNameResultCB(devCount, camName);
                            }

                            //摄像头分辨率返回
                            if (rDataArr[2] == 0x51) {
                                console.log("摄像头分辨率返回==============")
                                var resCount = rDataArr[3];
                                var resArr = new Array();
                                for (var i = 0; i < resCount; i++) {
                                    var width = rDataArr[4 + i * 4 + 0] * 256 + rDataArr[4 + i * 4 + 1];
                                    var height = rDataArr[4 + i * 4 + 2] * 256 + rDataArr[4 + i * 4 + 3];
                                    var resStr = "" + width + "*" + height;
                                    resArr.push(resStr);
                                }
                                that.GetResolutionResultCB(resCount, resArr);
                            }

                            //摄像头开启状态返回
                            if (rDataArr[2] == 0x01) {
                                console.log("摄像头开启====")
                                if (rDataArr[3] == 0x01) {
                                    that.isOpenMainCamera = true;
                                    that.GetCameraOnOffStatus(0);
                                }
                                if (rDataArr[3] == 0x03) {
                                    that.isOpenMainCamera = false;
                                    that.GetCameraOnOffStatus(1);
                                }
                            }


                            if (rDataArr[2] == 0xA5) {
                                console.log("未知-----------")
                                var flag;
                                if (rDataArr[3] == 0x00) {
                                    flag = 2;
                                }
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;
                                }
                                if (flag == 0) {
                                    var ResultStr = "";
                                    var pDataLen = rDataArr.length - 4;
                                    if (pDataLen > 0) {
                                        var pData = new Uint8Array(pDataLen);
                                        for (var i = 0; i < pDataLen; i++) {
                                            pData[i] = rDataArr[4 + i];
                                        }
                                        var str = that.byteToString(pData);
                                        console.log(str);
                                        ResultStr = decodeURIComponent(str);
                                    }
                                    console.log(ResultStr);
                                }
                            }


                            //当前拍照状态返回
                            if (rDataArr[2] == 0x10) {
                                //console.log(rDataArr[3]);
                                //GetCaptrueStatusResultCB(rDataArr[3]);
                            }



                            //拍照结果返回
                            if (rDataArr[2] == 0x10) {

                                var flag;
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;
                                    var imgpathLen = rDataArr[4] * 256 + rDataArr[5];
                                    if (imgpathLen == 0) {
                                        var base64Len = rDataArr[6] * 65536 + rDataArr[7] * 256 + rDataArr[8];
                                        var imgPathStr = "";
                                        var base64Data = new Uint8Array(base64Len);
                                        for (var i = 0; i < base64Len; i++) {
                                            base64Data[i] = rDataArr[9 + imgpathLen + i];
                                        }
                                        var base64Str = that.Uint8ArrayToString(base64Data);
                                        that.GetCaptrueImgResultCB(flag, imgPathStr, base64Str);
                                    }
                                    else {
                                        var base64Len = rDataArr[6] * 65536 + rDataArr[7] * 256 + rDataArr[8];
                                        var pData = new Uint8Array(imgpathLen);
                                        for (var i = 0; i < imgpathLen; i++) {
                                            pData[i] = rDataArr[9 + i];
                                        }
                                        var str = that.byteToString(pData);
                                        var imgPathStr = decodeURIComponent(str);

                                        var base64Data = new Uint8Array(base64Len);
                                        for (var i = 0; i < base64Len; i++) {
                                            base64Data[i] = rDataArr[9 + imgpathLen + i];
                                        }
                                        var base64Str = that.Uint8ArrayToString(base64Data);

                                        that.GetCaptrueImgResultCB(flag, imgPathStr, base64Str);
                                    }
                                }
                                if (rDataArr[3] == 0x02) {
                                    flag = 2;
                                    that.GetCaptrueImgResultCB(flag, "", "");
                                }

                            }

                            //身份证信息返回
                            if (rDataArr[2] == 0x80) {
                                var flag;
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;
                                }
                                if (rDataArr[3] == 0x02) {
                                    flag = 2;
                                }
                                var pDataLen = rDataArr.length - 4;
                                var pData = new Uint8Array(pDataLen);
                                for (var i = 0; i < pDataLen; i++) {
                                    pData[i] = rDataArr[4 + i];
                                }
                                var str = that.byteToString(pData);
                                var AllInfoStr = decodeURIComponent(str);
                                var resultStr = new Array();
                                resultStr = AllInfoStr.split(';');
                                //console.log(resultStr);
                                that.GetIdCardInfoResultCB(flag, resultStr[0], resultStr[1], resultStr[2], resultStr[3], resultStr[4], resultStr[5], resultStr[6], resultStr[7], resultStr[8], resultStr[9]);
                            }


                            //身份证信息及证件合并照片信息返回
                            if (rDataArr[2] == 0x81) {
                                var flag;
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;
                                }
                                if (rDataArr[3] == 0x02) {
                                    flag = 2;
                                }

                                var pDataLen = rDataArr.length - 4;
                                var pData = new Uint8Array(pDataLen);
                                for (var i = 0; i < pDataLen; i++) {
                                    pData[i] = rDataArr[4 + i];
                                }
                                var str = that.byteToString(pData);
                                var AllInfoStr = decodeURIComponent(str);
                                var resultStr = new Array();
                                resultStr = AllInfoStr.split(';');
                                that.GetIdCardInfoAndImgResultCB(flag, resultStr[0], resultStr[1], resultStr[2], resultStr[3], resultStr[4], resultStr[5], resultStr[6], resultStr[7], resultStr[8], resultStr[9], resultStr[10]);
                            }

                            //上传结果返回
                            if (rDataArr[2] == 0x90) {
                                var flag;
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;
                                }
                                if (rDataArr[3] == 0x02) {
                                    flag = 2;
                                }
                                if (rDataArr[3] == 0x03) {
                                    flag = 3;
                                }
                                var pDataLen = rDataArr.length - 6;
                                if (pDataLen <= 0) {
                                    that.HttpResultCB(flag, ResultStr);
                                }
                                var pData = new Uint8Array(pDataLen);
                                for (var i = 0; i < pDataLen; i++) {
                                    pData[i] = rDataArr[6 + i];
                                }
                                var str = that.byteToString(pData);
                                console.log(str);
                                var ResultStr = decodeURIComponent(str);
                                console.log(ResultStr);
                                that.HttpResultCB(flag, ResultStr);

                            }

                            if (rDataArr[2] == 0xC2) {
                                var flag, proValue;
                                flag = rDataArr[3];
                                proValue = rDataArr[4];
                                var ResultStr = "";

                                console.log("result:" + flag + "_proValue:" + proValue);

                                if (flag == 0) {
                                    var pDataLen = rDataArr.length - 7;
                                    var pData = new Uint8Array(pDataLen);
                                    for (var i = 0; i < pDataLen; i++) {
                                        pData[i] = rDataArr[7 + i];
                                    }
                                    var str = that.byteToString(pData);
                                    console.log(str);
                                    ResultStr = decodeURIComponent(str);
                                    console.log(ResultStr);
                                    OcrCallResultCB(flag, proValue, ResultStr);
                                }
                                else {
                                    OcrCallResultCB(flag, proValue, ResultStr);
                                }

                            }


                            //条码二维码识别结果返回
                            if (rDataArr[2] == 0x91) {
                                var flag;
                                if (rDataArr[3] == 0x00) {
                                    flag = 2;    //未识别到内容
                                }
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;    //识别到内容
                                }
                                var ResultStr = "";
                                var pDataLen = rDataArr.length - 4;
                                if (flag == 0 && pDataLen > 0) {
                                    var pData = new Uint8Array(pDataLen);
                                    for (var i = 0; i < pDataLen; i++) {
                                        pData[i] = rDataArr[4 + i];
                                    }
                                    var str = that.byteToString(pData);
                                    ResultStr = decodeURIComponent(str);
                                }
                                console.log(ResultStr);
                                that.QrBarCodeRecogResultCB(flag, ResultStr);

                            }


                            //添加合并PDF文件结果返回
                           /* if (rDataArr[2] == 0x31) {
                                var flag;
                                var base64Len = rDataArr.length - 4;
                                var base64Str = "";
                                if (base64Len > 0) {
                                    var base64Data = new Uint8Array(base64Len);
                                    for (var i = 0; i < base64Len; i++) {
                                        base64Data[i] = rDataArr[4 + i];
                                    }
                                    base64Str = this.Uint8ArrayToString(base64Data);
                                }
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;
                                }
                                if (rDataArr[3] == 0x02) {
                                    flag = 2;
                                }
                                this.AddImgFileToPDFResultCB(flag, base64Str);
                            }*/


                            //合并PDF结果返回
                            /*if (rDataArr[2] == 0x32) {
                                var flag;
                                var base64Len = rDataArr.length - 4;
                                var base64Str = "";
                                if (base64Len > 0) {
                                    var base64Data = new Uint8Array(base64Len);
                                    for (var i = 0; i < base64Len; i++) {
                                        base64Data[i] = rDataArr[4 + i];
                                    }
                                    base64Str = this.Uint8ArrayToString(base64Data);
                                }
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;
                                }
                                if (rDataArr[3] == 0x02) {
                                    flag = 2;
                                }
                                PdfCombineResultCB(flag, base64Str);
                            }*/



                            //添加合并图像文件结果返回
                            /*if (rDataArr[2] == 0xC0) {
                                var flag;
                                var base64Len = rDataArr.length - 4;
                                var base64Str = "";
                                if (base64Len > 0) {
                                    var base64Data = new Uint8Array(base64Len);
                                    for (var i = 0; i < base64Len; i++) {
                                        base64Data[i] = rDataArr[4 + i];
                                    }
                                    base64Str = this.Uint8ArrayToString(base64Data);
                                }
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;
                                }
                                if (rDataArr[3] == 0x02) {
                                    flag = 2;
                                }
                                AddMergeImageFileResultCB(flag, base64Str);
                            }

                            //合并图像结果返回
                            if (rDataArr[2] == 0xC1) {
                                var flag;
                                var base64Len = rDataArr.length - 4;
                                var base64Str = "";
                                if (base64Len > 0) {
                                    var base64Data = new Uint8Array(base64Len);
                                    for (var i = 0; i < base64Len; i++) {
                                        base64Data[i] = rDataArr[4 + i];
                                    }
                                    base64Str = this.Uint8ArrayToString(base64Data);
                                }
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;
                                }
                                if (rDataArr[3] == 0x02) {
                                    flag = 2;
                                }
                                console.log("MergeImagesResultCB 01");
                                MergeImagesResultCB(flag, base64Str);
                                console.log("MergeImagesResultCB 02");
                            }*/


                            /*if (rDataArr[2] == 0xA5) {
                                var flag;
                                if (rDataArr[3] == 0x00) {
                                    flag = 2;
                                }
                                if (rDataArr[3] == 0x01) {
                                    flag = 0;
                                }
                                var ResultStr = "";
                                if (flag == 0) {
                                    var pDataLen = rDataArr.length - 4;
                                    if (pDataLen > 0) {
                                        var pData = new Uint8Array(pDataLen);
                                        for (var i = 0; i < pDataLen; i++) {
                                            pData[i] = rDataArr[4 + i];
                                        }
                                        var str = this.byteToString(pData);
                                        ResultStr = decodeURIComponent(str);
                                    }
                                    // console.log(ResultStr);
                                }
                                GetLicenseResultCB(ResultStr);
                            }*/


                            //获取驱动盘符
                            /*if (rDataArr[2] == 0xA6) {
                                var strLen = rDataArr.length - 3;
                                if (strLen > 0) {
                                    var driveData = new Uint8Array(strLen);
                                    for (var i = 0; i < strLen; i++) {
                                        driveData[i] = rDataArr[3 + i];
                                    }
                                    var driveStr = this.Uint8ArrayToString(driveData);
                                    GetDriveResultCB(driveStr);
                                }
                                else {
                                    GetDriveResultCB("");
                                }
                            }*/


                            //删除文件结果
                            /*if (rDataArr[2] == 0xA8) {
                                var flag;
                                if (rDataArr[3] == 0) {
                                    flag = 0;
                                }
                                else {
                                    flag = 2;
                                }
                                GetDeleteFileResultCB(flag);
                            }*/



                            //摄像头数据
                            if (rDataArr[2] == 0xcc) {
                                var ww = rDataArr[3] * 256 + rDataArr[4];
                                var hh = rDataArr[5] * 256 + rDataArr[6];
                                that.pMainShowStartX = rDataArr[7] * 256 + rDataArr[8];
                                that.pMainShowStartY = rDataArr[9] * 256 + rDataArr[10];
                                that.MainContext.clearRect(0, 0, that.MainCanvas.width, that.MainCanvas.height);
                                var imgData = that.MainContext.createImageData(ww, hh);
                                var dataNum = 0;
                                dataNum = dataNum + 11;
                                for (var i = 0; i < imgData.data.length; i += 4) {
                                    imgData.data[i + 0] = rDataArr[dataNum];
                                    imgData.data[i + 1] = rDataArr[dataNum + 1];
                                    imgData.data[i + 2] = rDataArr[dataNum + 2];
                                    imgData.data[i + 3] = 255;
                                    dataNum = dataNum + 3;
                                }

                                that.MainContext.putImageData(imgData, that.pMainShowStartX, that.pMainShowStartY);
                               
                                if (that.MainCamCutMode == 2) {
                                   // console.log(that.pALastX+"="+that.pALastY+"="+(that.pACurrentX - that.pALastX)+"="+(that.pACurrentY - that.pALastY))
                                    that.MainContext.strokeStyle = 'blue'; // 设置线条的颜色
                                    that.MainContext.lineWidth = 2; // 设置线条的宽度
                                    that.MainContext.beginPath(); // 绘制直线
                                    that.MainContext.rect(that.pALastX, that.pALastY, (that.pACurrentX - that.pALastX), (that.pACurrentY - that.pALastY));
                                    that.MainContext.closePath();
                                    that.MainContext.stroke();
                                }
                            }

                            rDataArr = null;

                        }
                    }



                };    
            }
            catch (ex) {
                alert("异常错误!") 
            }
        },
        byteToString(arr) {
            if (typeof arr === 'string') {
                return arr;
            }
            var str = '',
                    _arr = arr;
            for (var i = 0; i < _arr.length; i++) {
                var one = _arr[i].toString(2),
                        v = one.match(/^1+?(?=0)/);
                if (v && one.length == 8) {
                    var bytesLength = v[0].length;
                    var store = _arr[i].toString(2).slice(7 - bytesLength);
                    for (var st = 1; st < bytesLength; st++) {
                        store += _arr[st + i].toString(2).slice(2);
                    }
                    str += String.fromCharCode(parseInt(store, 2));
                    i += bytesLength - 1;
                } else {
                    str += String.fromCharCode(_arr[i]);
                }
            }
            return str;
        },
        Uint8ArrayToString(fileData) {
            var dataString = "";
            for (var i = 0; i < fileData.length; i++) {
                dataString += String.fromCharCode(fileData[i]);
            }
            return dataString
        },
        closess(){
            clearInterval(this.heartTimerId);
            clearInterval(this.intervalId);
        },
        closes(){
            this.$emit("closes","")
        },
        SetCutRect(rectX,rectY,rectW,rectH) {
            if (this.isSocketConnect) {
                var aDataArray = new Uint8Array(12);
                aDataArray[0] = 0x77;
                aDataArray[1] = 0x88;
                aDataArray[2] = 0x13;  //设置裁剪模式
                aDataArray[3] = 0x00;
                aDataArray[4] = rectX >> 8 & 0xff;
                aDataArray[5] = rectX & 0xff;
                aDataArray[6] = rectY >> 8 & 0xff;
                aDataArray[7] = rectY & 0xff;
                aDataArray[8] = rectW >> 8 & 0xff;
                aDataArray[9] = rectW & 0xff;
                aDataArray[10] = rectH >> 8 & 0xff;
                aDataArray[11] = rectH & 0xff;

                //console.log(aDataArray[8] + "," + aDataArray[9]);

                this.socket.send(aDataArray.buffer);
            }
        },
        CaptureImage(fileAddr) {

            if (this.isSocketConnect) {

                // var pathArray = stringToUint8Array(fileAddr);
                if (fileAddr == "") {
                    var packageCount = 1;
                    var len = 0;
                    var pindex = 0;
                    var totalLen =  12;  
                    var aDataArray = new Uint8Array(totalLen);
                    aDataArray[0] = 0x77;
                    aDataArray[1] = 0x88;
                    aDataArray[2] = 0x10;
                    aDataArray[3] = 0x00;
                    aDataArray[4] = len >> 16 & 0xff;
                    aDataArray[5] = len >> 8 & 0xff;
                    aDataArray[6] = len & 0xff;
                    aDataArray[7] = packageCount >> 8 & 0xff;   //包总数
                    aDataArray[8] = packageCount & 0xff;   //包总数
                    aDataArray[9] = 0;   //分包长度
                    aDataArray[10] = pindex >> 8 & 0xff;   //包序号
                    aDataArray[11] = pindex & 0xff;    //包序号
                    console.log("pindex:" + pindex);
                    this.socket.send(aDataArray.buffer);
                }
                

            }
        }
    },
    deactivated(){
        
        
     },
}
</script>